package com.enation.app.shop.front.policyInsurance.service.impl;

import com.enation.app.shop.core.goods.model.Cat;
import com.enation.app.shop.core.goods.model.Goods;
import com.enation.app.shop.core.goods.model.Product;
import com.enation.app.shop.front.policyInsurance.service.InpolicyMapper;
import com.enation.framework.database.IDaoSupport;
import com.enation.framework.database.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 保险管理接口
 */
@Service
public class InpolicyMapperService implements InpolicyMapper {
    @Autowired
    private IDaoSupport daoSupport;

    /**
     * 查询保险
     * @param
     * @return
     */
    @Override
    public Page getAllInspolicy(Map inPolicyMap, Integer page, Integer pageSize, String other, String order){
        String sql = createAllInPolicySql(inPolicyMap);
        if(other != null && order != null){
            sql+=" order by "+other+" "+order;
        }
        Page  webpage  = daoSupport.queryForPage(sql,page,pageSize);

        return webpage;
    }

    /**
     * 根据ID查看保险详情
     * @param goods_id
     * @return
     */
    @Override
    public int getInspolicyByNum(Integer goods_id) {
        String sql = "select * from es_goods e where e.goods_id='?'";
        return this.daoSupport.queryForInt(sql,goods_id);
    }

    /**
     * 添加保险
     * @param goods
     * @return
     */
    @Override
    public int addGoods(Goods goods,Product product) {
        this.daoSupport.insert("es_goods",goods);
        this.daoSupport.insert("es_product",product);
        return 1;
    }

    @Override

    public int addProduct(Product product){
        this.daoSupport.insert("es_product",product);
        return 1;
    };

    /**
     * 编辑保险
     * @param goods
     * @return
     */
    @Override
    public boolean updateGoods(Goods goods,Product product) {
        this.daoSupport.update("es_goods",goods,"goods_id="+goods.getGoods_id());
        this.daoSupport.update("es_update",product,"product_id="+product.getProduct_id());
        return true;
    }

    @Override
    public int delGoods(Integer goods_id) {
        String sql = "delet from es_goods where goods_id=?";
        return 0;
    }

    @Override
    public List<Cat> getInPolicyCat(){
        String sql = "select * from es_goods_cat c where c.cat_id in (231,244)";
        return daoSupport.queryForList(sql,Cat.class);
    }

    public String createAllInPolicySql(Map inpolicyMap){
        Integer supplier_id=(Integer) inpolicyMap.get("supplier_id");
        Integer cat_id = (Integer) inpolicyMap.get("cat_id");
        String name = (String) inpolicyMap.get("name");
        Integer province_id = (Integer) inpolicyMap.get("province_id");
        Integer city_id = (Integer) inpolicyMap.get("city_id");
        String item = (String) inpolicyMap.get("item");
        Integer region_id = (Integer) inpolicyMap.get("region_id");

        String sql = "select * from es_goods e where e.type_id=69 and e.disabled=0";

        if(supplier_id!=null){
            sql += " and e.supplier_id="+supplier_id;
        }
        if(cat_id!=null){
            sql += " and e.cat_id="+cat_id;
        }
        if(name!=null){
            sql += " and e.name like(%"+name+"%)";
        }
        if(province_id!=null&&province_id!=0){
            sql += " and e.province_id="+province_id;
        }
        if(city_id!=null&&city_id!=0){
            sql += " and e.city_id="+city_id;
        }
        if(region_id!=null&&region_id!=0){
            sql += " and e.region_id="+region_id;
        }
        if(item!=null){
            sql += " and e.item like(%"+item+"%)";
        }
        return sql;
    }
}
